package service;

import java.util.HashMap;
import java.util.Map;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.google.gson.Gson;
import com.google.gson.JsonArray;
import com.google.gson.JsonElement;

import bean.Admin;
import mapper.AdminMapper;
import mapper.Mapper;

public class AdminService extends Mapper<AdminMapper> {
	public String getAllJson(String spage, String username, String id) {
//		int page = Integer.parseInt(spage);
//		Page<Object> pageHelper = PageHelper.startPage(page, 10);
		
		Gson gson = new Gson();
		Map<String, Object> tempMap = new HashMap<>();
		tempMap.put("username", username);
		tempMap.put("id", id);
		JsonElement jsonTree = gson.toJsonTree(getMapper().findAll(tempMap));
		
//		int total = (int) pageHelper.getTotal();
		JsonArray jsonArray = jsonTree.getAsJsonArray();
		tempMap.clear();
//		tempMap.put("total", total);
		jsonArray.add(gson.toJsonTree(tempMap));
		return gson.toJson(jsonArray);
	}
	public void addAdmin(String username, String password) {
		Admin admin = new Admin(username, password);
		getMapper().addAdmin(admin);
	}
	public void altAdmin(String id, String username, String password) {
		Admin admin = new Admin(username, password);
		admin.setId(Integer.parseInt(id));
		getMapper().altAdmin(admin);
	}
	public void lockAdmin(String id, String lock) {
		Map<String, Object> map = new HashMap<>();
		map.put("id", Integer.parseInt(id));
		map.put("lock", lock);
		getMapper().lockAdmin(map);
	}
	public void delAdmin(String id) {
		getMapper().delAdmin(Integer.parseInt(id));
	}
}
